package com.moon.game.profile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import com.moon.game.clazz.PlayerItemsProto.PlayerItems;
import com.moon.game.database.MongoUtil;
import com.moon.game.exception.GException;
import com.moon.game.resource.model.Item;
import com.moon.game.util.Util;
import com.moon.game.vos.PlayerVo;

public class ProfileService {
	private static final String TAG_PLAYER = "player";
	private static final String TAG_PLAYER_ITEM = "player_item";
	private static final String TAG_ITEM = "item";
	private static final Logger DBLog = Logger.getLogger(ProfileService.class);
	
	/**
	 * 散列规则
	 * 
	 * @param playerId
	 * @param profileNum
	 * @return
	 */
	private static int hashRules(String playerId,int profileNum) {
		int index = (int)(Math.abs((playerId).hashCode()) % profileNum);
		return index;
	}
	/**
	 * 保存人物到mongo
	 * 
	 * @param playerId
	 * @param player
	 * @throws GException 
	 */
	public static void savePlayer2DB(String sid,HashMap<String, PlayerVo> playerMap) throws GException {
		int index = hashRules(sid,ProfileConfig.getPlayerProfileNum());
		try {
			DBObject data = (DBObject)JSON.parse(Util.serialToJson(playerMap));
			MongoUtil.saveData2Db(ProfileConfig.getPlayerProfile(index), sid, TAG_PLAYER, TAG_PLAYER, data);
		} catch (Exception e) {
			throw new GException(e);
		}
	}
	
	
	/**
	 * 从mongo中获取人物信息
	 * 
	 * @param playerId
	 * @return
	 * @throws GException
	 */
	public static HashMap<String, PlayerVo> getPlayerFromDB(String sid) throws GException {
		int index = hashRules(sid,ProfileConfig.getPlayerProfileNum());
		try {
			String data = MongoUtil.getDataFromDb(ProfileConfig.getPlayerProfile(index), sid, TAG_PLAYER, TAG_PLAYER);
			@SuppressWarnings("unchecked")
			HashMap<String, PlayerVo> dbPlayerMap = (HashMap<String, PlayerVo>) Util.jsonToSerialObject(data, new HashMap<String, PlayerVo>());
			return dbPlayerMap;
		} catch (Exception e) {
			DBLog.warn("sid:" + sid + "获取人物异常！！");
			e.printStackTrace();
			throw new GException(e.getMessage());
		}
	}
	
	/**
	 * 获取普通道具
	 * 
	 * @param playerId
	 * @return
	 * @throws GException
	 */
	public static ArrayList<Item> getItemsFromDB(String playerId) throws GException {
		int index = hashRules(playerId,ProfileConfig.getItemsProfileNum());
		try {
			String data = MongoUtil.getDataFromDb(ProfileConfig.getItemsProfile(index), playerId, TAG_ITEM, TAG_ITEM);
			ArrayList<Item> items = new ArrayList<Item>();
			if (data != null) {
				@SuppressWarnings("unchecked")
				List<Item> dbItemList = (List<Item>) Util.jsonToSerialObject(data, new ArrayList<Item>());
				Gson gson = new Gson();
				if (dbItemList != null) {
					for (Object obj : dbItemList) {
						String json = gson.toJson(obj);
						Item item = gson.fromJson(json, Item.class);
						items.add(item);
					}
				}
			}
			return items;
		} catch (Exception e) {
			DBLog.warn("playerid:" + playerId + "获取道具异常！！");
			e.printStackTrace();
			throw new GException(e.getMessage());
		}
	}
	
	/**
	 * 保存人物道具
	 * 
	 * @param playerId
	 * @param data
	 * @throws GException 
	 */
	public static void saveItems2DB(String playerId,List<Item> item) throws GException {
		int index = hashRules(playerId,ProfileConfig.getPlayerProfileNum());
		try {
			DBObject data = (DBObject)JSON.parse(Util.serialToJson(item));
			MongoUtil.saveData2Db(ProfileConfig.getItemsProfile(index), playerId, TAG_ITEM, TAG_ITEM, data);
		} catch (Exception e) {
			throw new GException(e);
		}
	}
	
	/**
	 * 获取人物playerItem道具
	 * 
	 * @param playerId
	 * @return
	 * @throws GException
	 */
	public static PlayerItems getPlayerItemsFromDB(String playerId) throws GException {
		int index = hashRules(playerId,ProfileConfig.getItemsProfileNum());
		try {
			String data = MongoUtil.getDataFromDb(ProfileConfig.getPlayerItemsProfile(index), playerId, TAG_PLAYER_ITEM, TAG_PLAYER_ITEM);
			return (PlayerItems)Util.jsonToGenerated(data, PlayerItems.newBuilder().build());
		} catch (Exception e) {
			DBLog.warn("playerid:" + playerId + "获取道具异常！！");
			e.printStackTrace();
			throw new GException(e.getMessage());
		}
	}
	
	/**
	 * 保存人物playerItem道具
	 * 
	 * @param playerId
	 * @param data
	 * @throws GException 
	 */
	public static void savePlayerItems2DB(String playerId,PlayerItems newPlayerItems) throws GException {
		int index = hashRules(playerId,ProfileConfig.getPlayerProfileNum());
		try {
			DBObject data = (DBObject)JSON.parse(Util.generatedToJson(newPlayerItems));
			MongoUtil.saveData2Db(ProfileConfig.getPlayerItemsProfile(index), playerId, TAG_PLAYER_ITEM, TAG_PLAYER_ITEM, data);
		} catch (Exception e) {
			throw new GException(e);
		}
	}
	
}
